#include "../main.h"

#define FILE_GET_ALL "curl http://192.168.188.1/cgi-bin/luci/file/upload/fileall"

void *send_ping_thread(void *args)
{
    int *sockfd = (int *)args;
    while (1)
    {
        send_ping(*sockfd);
        sleep(5);
    }
    return NULL;
}

void send_ping(int sockfd)
{
    cJSON *ping_json = NULL;
    char data[1024];
    ping_json = cJSON_CreateObject();
    cJSON_AddStringToObject(ping_json, "type", "DEVICE_FILE_PING");
    cJSON_AddStringToObject(ping_json, "clientId", get_routerid());
    char *buffer = cJSON_PrintUnformatted(ping_json);
    int msg_size = strlen(buffer);
    memset(data, 0, sizeof(data));
    memcpy(data, buffer, msg_size);

    if (ping_json != NULL)
    {
        cJSON_Delete(ping_json);
    }
    if (buffer != NULL)
    {
        free(buffer);
        buffer = NULL;
    }
    encode_msg(sockfd, data, msg_size);
}

//重命名文件
void rename_file(int sockfd, char *msg)
{
    cJSON *pRank = NULL, *pValue = NULL;
    pRank = cJSON_Parse(msg);
    if (pRank == NULL)
    {
        OUT_DEBUG(DEBUG, "json 解析异常");
        OUT_DEBUG(DEBUG, "msg:%s", msg);
    }
    else
    {
        char cmd[1024];
        char oldName[256];
        char newName[256];
        memset(cmd, 0, sizeof(cmd));
        memset(oldName, 0, sizeof(oldName));
        memset(newName, 0, sizeof(newName));
        pValue = cJSON_GetObjectItem(pRank, "srcFileName");
        if (pValue != NULL && pValue->type == cJSON_String)
        {
            memcpy(oldName, pValue->valuestring, sizeof(oldName));
        }
        pValue = cJSON_GetObjectItem(pRank, "dstFileName");
        if (pValue != NULL && pValue->type == cJSON_String)
        {
            memcpy(newName, pValue->valuestring, sizeof(newName));
        }
        if (strlen(oldName) && strlen(newName))
        {
            struct stat file;
            if (stat(oldName, &file) < 0)
            {
                send_result_info(sockfd, "DEVICE_FILE_RENAME_CHECK", "文件重命名失败");
            }
            else
            {
                memset(cmd, 0, sizeof(cmd));
                snprintf(cmd, sizeof(cmd), "mv %s %s", oldName, newName);
                system(cmd);
                send_result_info(sockfd, "DEVICE_FILE_RENAME_CHECK", "文件重命名成功");
            }
        }
        cJSON_Delete(pRank);
    }
}

//删除文件
void delete_file(int sockfd, char *msg)
{
    cJSON *pRank = NULL, *pValue = NULL;
    pRank = cJSON_Parse(msg);
    if (pRank == NULL)
    {
        OUT_DEBUG(DEBUG, "json 解析异常");
        OUT_DEBUG(DEBUG, "msg:%s", msg);
    }
    else
    {
        char cmd[1024];
        char file_path[256];
        pValue = cJSON_GetObjectItem(pRank, "filePath");
        if (pValue != NULL && pValue->type == cJSON_String)
        {
            memset(file_path, 0, sizeof(file_path));
            memcpy(file_path, pValue->valuestring, sizeof(file_path));

            struct stat file;
            if (stat(file_path, &file) < 0)
            {
                send_result_info(sockfd, "DEVICE_FILE_DELETE_CHECK", "文件不存在");
            }
            else
            {
                memset(cmd, 0, sizeof(cmd));
                snprintf(cmd, sizeof(cmd), "rm -f %s", file_path);
                system(cmd);
                send_result_info(sockfd, "DEVICE_FILE_DELETE_CHECK", "文件删除成功");
            }
        }

        cJSON_Delete(pRank);
    }
}

void send_result_info(int sockfd, char *cmdType, char *msg)
{
    cJSON *result_info = NULL;
    result_info = cJSON_CreateObject();
    cJSON_AddBoolToObject(result_info, "bResult", 1);
    cJSON_AddNumberToObject(result_info, "resultCode", 1);
    cJSON_AddStringToObject(result_info, "resultMsg", msg);

    cJSON *down_check = cJSON_CreateObject();
    cJSON_AddStringToObject(down_check, "type", cmdType);
    cJSON_AddStringToObject(down_check, "clientId", get_routerid());
    cJSON_AddStringToObject(down_check, "content", cJSON_PrintUnformatted(result_info));
    char *buffer = cJSON_PrintUnformatted(down_check);
    if (result_info != NULL)
    {
        cJSON_Delete(result_info);
    }
    if (down_check != NULL)
    {
        cJSON_Delete(down_check);
    }
    encode_msg(sockfd, buffer, strlen(buffer));
    free(buffer);
    buffer = NULL;
}

void *task_uploadfile(void *args)
{
    while (1)
    {
        char *result = NULL;
        result = shell_cmd_change(FILE_GET_ALL);
        if (result != NULL && strlen(result) > 0)
        {
            cJSON *para = NULL;
            para = cJSON_CreateObject();
            cJSON_AddStringToObject(para, "md5", get_routerid());
            cJSON_AddStringToObject(para, "itemstr", result);
            char *buffer = cJSON_PrintUnformatted(para);
            if (para != NULL)
            {
                cJSON_Delete(para);
            }

            char update_url[102400];
            memset(update_url, 0, sizeof(update_url));
            sprintf(update_url, "curl -H \"Content-Type: application/json\" -X POST  --data '%s' http://router.skywindlink.com/router/devicefile/update", buffer);
            char *result_update = shell_cmd_change(update_url);
            if (result_update != NULL)
            {
                int len = strlen(result_update);
                result_update[len] = '\0';
                OUT_DEBUG(DEBUG, "result_update:%s ", result_update);
                free(result_update);
                result_update = NULL;
            }
            if (buffer != NULL)
            {
                free(buffer);
                buffer = NULL;
            }
        }

        if (result != NULL)
        {
            free(result);
            result = NULL;
        }
        sleep(5);
    }
}
